#ifndef CAMERADECKLINK_H
#define CAMERADECKLINK_H

#include "../../../../../../Blackmagic DeckLink SDK 9.7.1/Mac/include/DeckLinkAPI.h"

#include <FFGL.h>
#include <FFGLExtensions.h>

#include <stdio.h>
#include <pthread.h>
#include <map>
#include <vector>
#include <deque>

class DeckLinkCaptureDelegate;

class CameraDecklink
{
public:
    CameraDecklink();
    ~CameraDecklink();

    bool initializeCamera(IDeckLink * _deckLink);
    bool startCapture();
    void stopCapture();

	void* getFrameBytes();

    bool fetchSelectedDisplayMode(int modeIndex);

	bool isRunning() { return m_Running; }
	
	DeckLinkCaptureDelegate*        m_Delegate;
	
	bool FrameHasBeenUpdated();
	
private:
    bool m_Running;
	
    IDeckLinkInput*                 m_DeckLinkInput;
    IDeckLinkDisplayMode*           m_DisplayMode;
    BMDVideoInputFlags              m_InputFlags;
    BMDDisplayMode                  m_SelectedDisplayMode;
    BMDPixelFormat                  m_PixelFormat;
    int                             m_ExitStatus;
    HRESULT                         m_Result;
};

class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
{
public:
    DeckLinkCaptureDelegate();
    ~DeckLinkCaptureDelegate();

    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
    virtual ULONG STDMETHODCALLTYPE AddRef(void);
    virtual ULONG STDMETHODCALLTYPE  Release(void);
    virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents,
                                                              IDeckLinkDisplayMode*,
                                                              BMDDetectedVideoInputFormatFlags);
    virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*,
                                                             IDeckLinkAudioInputPacket*);

    pthread_mutex_t		*sleepMutex;
    pthread_cond_t		*sleepCond;
	bool stopped;
    void *frameBytes;
	uint read_buffer, write_buffer;
   
	// long frameCount;
    BMDTimecodeFormat	g_timecodeFormat;

	pthread_mutex_t		framebytesMutex;

	bool FrameHasBeenUpdated;
private:
    ULONG				m_refCount;
    pthread_mutex_t		m_mutex;

    int height, width;
	
	bool m_DropFrame;
};


#endif // CAMERADECKLINK_H
